<?php

/**
 * Catalog manage products block
 *
 * @category   VES
 * @package    VES_Vendors
 * @author     Vnecoms Team <support@vnecoms.com>
 */
class VES_VendorsProduct_Block_Adminhtml_Catalog_Product_Pending_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
	protected function _prepareMassaction()
    {

        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('product');
		$this->getMassactionBlock()->addItem('approve', array(
             'label'=> Mage::helper('vendorsproduct')->__('Approve'),
             'url'  => $this->getUrl('*/*/massApprove'),
        ));
		$this->getMassactionBlock()->addItem('1stapprove', array(
             'label'=> Mage::helper('vendorsproduct')->__('1ST Approve'),
             'url'  => $this->getUrl('*/*/mass1stapprove'),
        ));
		$this->getMassactionBlock()->addItem('2ndapprove', array(
             'label'=> Mage::helper('vendorsproduct')->__('2ND Approve'),
             'url'  => $this->getUrl('*/*/mass2ndapprove'),
        ));
//      $this->getMassactionBlock()->addItem('3rdapprove', array(
//            'label'=> Mage::helper('vendorsproduct')->__('3RD Approve'),
//            'url'  => $this->getUrl('*/*/mass3rdapprove'),
//       ));
/* you 翻译接口提交处理 2016-07-20 */
        $this->getMassactionBlock()->addItem('3rdapprove', array(
             'label'=> Mage::helper('vendorsproduct')->__('3RD Approve'),
             'url'  => $this->getUrl('tranpronduct/adminhtml_transla/gettranslasubmi/'),
        ));
/* you 翻译接口提交处理 2016-07-20 */
        
        $this->getMassactionBlock()->addItem('reject', array(
             'label'=> Mage::helper('vendorsproduct')->__('Reject'),
             'url'  => $this->getUrl('*/*/massReject'),
        ));
//		$this->getMassactionBlock()->addItem('delete', array(
//             'label'=> Mage::helper('vendorsproduct')->__('Delete'),
//             'url'  => $this->getUrl('*/*/massDelete'),
//             'confirm' => Mage::helper('catalog')->__('Are you sure?')
//        ));
        Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
        return $this;
    }
    
    protected function _prepareCollection()
    {
    	$store = $this->_getStore();
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('attribute_set_id')
            ->addAttributeToSelect('type_id')
			->addAttributeToSelect('special_price')
            ->addAttributeToFilter('approval',array(
                'in'=>array(
                        VES_VendorsProduct_Model_Source_Approval::STATUS_PENDING,
                        VES_VendorsProduct_Model_Source_Approval::STATUS_1STAPPROVED,
                        VES_VendorsProduct_Model_Source_Approval::STATUS_2NDAPPROVED,
                        VES_VendorsProduct_Model_Source_Approval::STATUS_3RDAPPROVED,
                )));
		$collection->joinTable(array('vendor_table'=>'vendors/vendor'),'entity_id = vendor_id',array('vendor'=>'vendor_id'));
        if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
            $collection->joinField('qty',
                'cataloginventory/stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left');
        }
        if ($store->getId()) {
            //$collection->setStoreId($store->getId());
            $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
            $collection->addStoreFilter($store);
            $collection->joinAttribute(
                'name',
                'catalog_product/name',
                'entity_id',
                null,
                'inner',
                $adminStore
            );
            $collection->joinAttribute(
                'custom_name',
                'catalog_product/name',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'status',
                'catalog_product/status',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'visibility',
                'catalog_product/visibility',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'price',
                'catalog_product/price',
                'entity_id',
                null,
                'left',
                $store->getId()
            );
        }
        else {
            $collection->addAttributeToSelect('price');
            $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
            $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        }

        $this->setCollection($collection);

        Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
        $this->getCollection()->addWebsiteNamesToResult();
    	return $this;
    }
    
    
    protected function _prepareColumns(){
		parent::_prepareColumns();
		$this->_rssLists = array();
		
		/* lie */
		$this->removeColumn('websites');
		/* lie */
		
	
		$renderer = new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_PendingImage();
    	$this->addColumnAfter('thumbnail',
            array(
                'header'=> Mage::helper('catalog')->__('Thumbnail'),
                'index' => 'entity_id',
            	'type'  => 'text',
            	'width'	=> '100px',
            	'renderer'	=> $renderer,
            	'filter'    => false,
                'sortable'  => false,
        ),'name');
		
		$this->addColumnAfter('vendor',
            array(
                'header'=> Mage::helper('vendorsproduct')->__('Vendor'),
                'index' => 'vendor',
            	'renderer'	=> new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_Text(),
        ),'entity_id');

		$this->addColumnAfter('vendorcompany',
	    array(
	        'header'=> Mage::helper('vendorsproduct')->__('Company'),
	        'index' => 'vendor',
	        'renderer'  => new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_VendorCompanyText(),
	    ),'vendor');

		$this->addColumnAfter('vendorbrand',
	    array(
	        'header'=> Mage::helper('vendorsproduct')->__('Brand'),
	        'index' => 'entity_id',
	        'renderer'  => new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_VendorBrand(),
	    ),'vendorcompany');
		
		
		$store = $this->_getStore();
		$this->addColumnAfter('special_price',
	            array(
	                'header'=> Mage::helper('vendors')->__('Speical Price'),
					'type'  => 'price',
					'currency_code' => $store->getBaseCurrency()->getCode(),
					'index' => 'special_price',
	        	),
	    'price');
		
        $this->addColumnAfter('vendorplatformprice',
            array(
                'header'=> Mage::helper('vendorsproduct')->__('Platform Price'),
                'type'  => 'number',
                'index' => 'entity_id',
				'renderer'  => new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_VendorPlatform(),
        ),'special_price');
		
		$this->addColumnAfter('create_at',
            array(
                'header'    => Mage::helper('vendorsproduct')->__('Date'),
				'align'     => 'right',
				'type'      => 'datetime',
				'format'	=> 'Y-M-d H:m:s',
				'index'     => 'created_at',
        ),'visibility');
		
		
    	$this->addColumnAfter('approval',
            array(
                'header'=> Mage::helper('catalog')->__('Approval'),
                'index' => 'approval',
            	'type'  => 'options',
                'options' 	=> Mage::getSingleton('vendorsproduct/source_approval')->getOptionArray(),
            	'renderer'	=> new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_Approval(),
        ),'entity_id');
        
/* you 三审不能编辑，不显示action选项 2016-07-21 */	
       // $acionData = array(
       //      array(
       //          'caption' => Mage::helper('catalog')->__('Edit'),
       //          'url'     => array(
       //              'base'=>'adminhtml/catalog_product/edit',
       //              'params'=>array('store'=>$this->getRequest()->getParam('store'))
       //          ),
       //          'field'   => 'id',
       //      )
       //  );
       //  $this->getColumn('action')->setData('actions',$acionData);
        $this->addColumn('action',
            array(
                'header'    => Mage::helper('catalog')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('catalog')->__('Edit'),
                        'url'     => array(
                            'base'=>'adminhtml/catalog_product/edit',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    )
                ),
                'renderer' => new VES_VendorsProduct_Block_Widget_Grid_Column_Renderer_TranslateActions(),
                /*'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',*/
        ));
/* you 三审不能编辑，不显示action选项 2016-07-21 */

		return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
    }
    
	public function getRowUrl($row)
    {
        $approval = $row->getApproval();
        if($this->isInTranslate($approval))
        {
            return $this->getUrl('adminhtml/catalog_product/edit', array(
                'store'=>$this->getRequest()->getParam('store'),
                'id'=>$row->getId())
            );             
        }else{
           return false;
        }       
    }

/* you 是否是三审(翻译中)产品 2016-07-21 */
    protected function isInTranslate($approval){
        if($approval == VES_VendorsProduct_Model_Source_Approval::STATUS_3RDAPPROVED){
            return false;
        }else{
            return true;
        }
    }
/* you 是否是三审(翻译中)产品 2016-07-21 */
}
